<?php

/**
 * 
 * ClientEngage: ClientEngage Visitor Voice (http://www.clientengage.com)
 * Copyright 2012, ClientEngage (http://www.clientengage.com)
 *
 * You must have purchased a valid license from CodeCanyon in order to have 
 * the permission to use this file.
 * 
 * You may only use this file according to the respective licensing terms 
 * you agreed to when purchasing this item on CodeCanyon.
 * 
 * 
 * 
 *
 * @author          ClientEngage <contact@clientengage.com>
 * @copyright       Copyright 2013, ClientEngage (http://www.clientengage.com)
 * @link            http://www.clientengage.com ClientEngage
 * @since           ClientEngage - Visitor Voice v 1.0
 * 
 */
App::uses('AppController', 'Controller');

/**
 * Buttonthemes Controller
 *
 * @property Buttontheme $Buttontheme
 */
class ButtonthemesController extends AppController
{
    
    public $name = 'Buttonthemes';

    /**
     * index method
     *
     * @return void
     */
    public function admin_index()
    {
        $this->set('title_for_layout', __('Button Themes'));

        $this->Buttontheme->recursive = 0;
        $this->set('buttonthemes', $this->paginate());
    }

    /**
     * add method
     *
     * @return void
     */
    public function admin_add()
    {
        $this->set('title_for_layout', __('Add Button Theme'));

        if ($this->request->is('post'))
        {
            $this->Buttontheme->create();
            if ($this->Buttontheme->save($this->request->data))
            {
                $this->Session->setFlash(__('The Button Theme has been saved'), Flash::Success);
                $this->redirect(array('action' => 'edit', $this->Buttontheme->id));
            }
            else
            {
                $this->Session->setFlash(__('The Button Theme could not be saved. Please, try again.'), Flash::Warning);
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null)
    {
        $this->set('title_for_layout', __('Edit Button Theme'));

        $this->Buttontheme->id = $id;
        if (!$this->Buttontheme->exists())
        {
            throw new NotFoundException(__('Invalid Button Theme'));
        }
        if ($this->request->is('post') || $this->request->is('put'))
        {
            if ($this->Buttontheme->save($this->request->data))
            {
                $this->Session->setFlash(__('The Button Theme has been saved'), Flash::Success);
                $this->redirect(array('action' => 'edit', $this->Buttontheme->id));
            }
            else
            {
                $this->Session->setFlash(__('The Button Theme could not be saved. Please, try again.'), Flash::Warning);
            }
        }
        else
        {
            $this->request->data = $this->Buttontheme->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null)
    {
        if (!$this->request->is('post'))
        {
            throw new MethodNotAllowedException();
        }
        $this->Buttontheme->id = $id;
        if (!$this->Buttontheme->exists())
        {
            throw new NotFoundException(__('Invalid Button Theme'));
        }

        if ($this->Buttontheme->Visitorform->hasAny(array('Visitorform.buttontheme_id' => $id)))
        {
            $this->Session->setFlash(__('This Button Theme is still associated to one or more Visitor Forms.'), Flash::Error);
            $this->redirect($this->referer());
            return;
        }

        if ($this->Buttontheme->delete())
        {
            $this->Session->setFlash(__('Button Theme deleted'), Flash::Success);
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Button Theme was not deleted'), Flash::Error);
        $this->redirect(array('action' => 'index'));
    }

}
